<div id="content">
    <h3><a href="../index.php?p=home">Geoplaza</a><a href="index.php?p=home"> &rsaquo;&rsaquo; Onderwijs</a> &rsaquo;&rsaquo; Mijn profiel bewerken</h3>
    <div id="Profilehead">Profiel bewerken</div>
    <div id="profile">
        <?php
        require_once('../config/appvars.php');
        require_once('../config/config.php');

        // Make sure the user is logged in before going any further.
        if (!isset($_SESSION['user_id']) && $_SESSION['pakket'] == 'b') {
            echo '<p class="login">Please <a href="../index.php?p=home">log in</a> to access this page.</p>';
            exit();
        }
        
        if ($_SESSION['pakket'] == 'bb') {
            // Connect to the database
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

            if (isset($_POST['submit'])) {
                // Grab the profile data from the POST
                $instellingnaam = mysqli_real_escape_string($dbc, trim($_POST['instellingnaam']));
                $adres = mysqli_real_escape_string($dbc, trim($_POST['adres']));
                $postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));
                $woonplaats = mysqli_real_escape_string($dbc, trim($_POST['woonplaats']));
                $provincie = mysqli_real_escape_string($dbc, trim($_POST['provincie']));
                $telefoon = mysqli_real_escape_string($dbc, trim($_POST['telefoon']));
                $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
                $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
                $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
                $new_picture_type = $_FILES['new_picture']['type'];
                $new_picture_size = $_FILES['new_picture']['size'];
                list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);
                $error = false;
                // Validate and move the uploaded picture file, if necessary
                if (!empty($new_picture)) {
                    if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
                            ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&
                            ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
                        if ($_FILES['new_picture']['error'] == 0) {
                            // Move the file to the target upload folder
                            $target = MM_UPLOADPATH . basename($new_picture);
                            if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
                                // The new picture file move was successful, now make sure any old picture is deleted
                                if (!empty($old_picture) && ($old_picture != $new_picture)) {
                                    @unlink(MM_UPLOADPATH . $old_picture);
                                }
                            } else {
                                // The new picture file move failed, so delete the temporary file and set the error flag
                                @unlink($_FILES['new_picture']['tmp_name']);
                                $error = true;
                                echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
                            }
                        }
                    } else {
                        // The new picture file is not valid, so delete the temporary file and set the error flag
                        @unlink($_FILES['new_picture']['tmp_name']);
                        $error = true;
                        echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
                        ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
                    }
                }


                // Update the profile data in the database
                if (!$error) {


                    if (!empty($instellingnaam) && !empty($adres) && !empty($postcode) && !empty($woonplaats) && !empty($provincie) && !empty($telefoon) && !empty($email)) {

                        if (!empty($new_picture)) {
                            $query = "UPDATE onderwijsinstelling SET instellingNaam = '$instellingnaam', 
                            adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', 
                            provincie = '$provincie', telefoon = '$telefoon', email = '$email',logo = '$new_picture' 
                            WHERE werkgeverID = '" . $_SESSION['user_id'] . "'";
                        } else {
                            $query = "UPDATE werkgeverprofiel SET instellingNaam = '$instellingnaam', 
                            adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', 
                            provincie = '$provincie', telefoon = '$telefoon', email = '$email',
                            WHERE werkgeverID = '" . $_SESSION['user_id'] . "'";
                        }

                        mysqli_query($dbc, $query);

                        $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=viewprofilebb';
                        header('Location: ' . $home_url);

                        mysqli_close($dbc);
                        exit();
                    } else {
                        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
                    }
                }
            } // End of check for form submission
            else {
                // Grab the profile data from the database
                $query = "SELECT instellingNaam, adres, postcode, woonplaats, provincie, telefoon, email, logo FROM onderwijsinstelling WHERE onderwijs_id = '" . $_SESSION['user_id'] . "'";
                $data = mysqli_query($dbc, $query);
                $row = mysqli_fetch_array($data);

                if ($row != NULL) {
                    $instellingnaam = $row['instellingNaam'];
                    $adres = $row['adres'];
                    $postcode = $row['postcode'];
                    $woonplaats = $row['woonplaats'];
                    $province = $row['provincie'];
                    $telefoon = $row['telefoon'];
                    $email = $row['email'];
                    $old_picture = $row['logo'];
                } else {
                    echo '<p class="error">There was a problem accessing your profile.</p>';
                }
            }
            mysqli_close($dbc);
        } else {
            echo '<p class="login">Please <a href="../index.php?p=home">log in</a> to access this page.</p>';
            exit();
        }
        ?>
        <form enctype="multipart/form-data" method="post" action="index.php?p=editprofilebb">
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
            <fieldset class="info">
                <legend>Onderwijs informatie</legend>
                <p class="titel"> Instellingnaam</p>
                <input name="instellingnaam" id="instellingnaam" class="edit" type="text" value="<?php if (!empty($instellingnaam))
            echo $instellingnaam; ?>"/>

                <p class="titel">Adres</p>
                <input name="adres" id="adres" class="edit" type="text" value="<?php if (!empty($adres))
                           echo $adres; ?>"/>

                <p class="titel">Postcode</p>
                <input name="postcode" id="postcode" class="edit" type="text" value="<?php if (!empty($postcode))
                           echo $postcode; ?>"/>

                <p class="titel">Woonplaats</p>
                <input name="woonplaats" id="woonplaats" class="edit" type="text"value="<?php if (!empty($woonplaats))
                           echo $woonplaats; ?>" />

                <p class="titel">Provincie</p>
                <select name="provincie" class="edit">
                    <option value="Groningen" <?php if (!empty($province) && $province == 'Groningen')
                           echo 'selected = "selected"'; ?>>Groningen</option>
                    <option value="Friesland" <?php if (!empty($province) && $province == 'Friesland')
                                echo 'selected = "selected"'; ?>>Friesland</option>
                    <option value="Drenthe" <?php if (!empty($province) && $province == 'Drenthe')
                                echo 'selected = "selected"'; ?>>Drenthe</option>
                    <option value="Overijssel" <?php if (!empty($province) && $province == 'Overijssel')
                                echo 'selected = "selected"'; ?>>Overijssel</option>
                    <option value="Flevoland" <?php if (!empty($province) && $province == 'Flevoland')
                                echo 'selected = "selected"'; ?>>Flevoland</option>
                    <option value="Gelderland" <?php if (!empty($province) && $province == 'Gelderland')
                                echo 'selected = "selected"'; ?>>Gelderland</option>
                    <option value="Utrecht" <?php if (!empty($province) && $province == 'Utrecht')
                                echo 'selected = "selected"'; ?> >Utrecht</option>
                    <option value="Noord-Holland" <?php if (!empty($province) && $province == 'Noord-Holland')
                                echo 'selected = "selected"'; ?>>Noord-Holland</option>
                    <option value="Zuid-Holland" <?php if (!empty($province) && $province == 'Zuid-Holland')
                                echo 'selected = "selected"'; ?>>Zuid-Holland</option>
                    <option value="Zeeland" <?php if (!empty($province) && $province == 'Zeeland')
                                echo 'selected = "selected"'; ?>>Zeeland</option>
                    <option value="Noord-Barabant" <?php if (!empty($province) && $province == 'Noord-Barabant')
                                echo 'selected = "selected"'; ?>>Noord-Barabant</option>
                    <option value="Limburg" <?php if (!empty($province) && $province == 'Limburg')
                                echo 'selected = "selected"'; ?>>Limburg</option>
                </select>

                <p class="titel">Telefoon</p>
                <input name="telefoon" id="telefoon" class="edit" type="text" value="<?php if (!empty($telefoon))
                                echo $telefoon; ?>" />

                     <p class="titel">Gebruikersnaam (je email)</p>
                     <input name="email" id="email" type="text" class="edit" value="<?php if (!empty($email))
                                echo $email; ?>"/>

                     <p class="insidetitel">Bedrijfslogo</p>
                     <p class="titel">Bedrijfslogo:</p>
                     <input type="hidden" name="old_picture" class="edit" value="<?php if (!empty($old_picture))
                                echo $old_picture; ?>" />
                     <input type="file" id="new_picture" class="edit" name="new_picture" />
                <?php
                            if (!empty($old_picture)) {
                                echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
                            }
                ?>

            </fieldset>
            <input type="submit" value="Save Profile" name="submit" />
        </form>
    </div>
</div>